package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.Result;
import com.itheima.pojo.FormDataAdvice;
import com.itheima.service.InterventionService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/intervention")
public class InterventionController {
    @Reference
    private InterventionService interventionService;

    /**
     * 添加干预数据
     * @param formDataAdvice
     * @param exerciseIds
     * @param foodIds
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody FormDataAdvice formDataAdvice, String[] exerciseIds, String[] foodIds){
        try {
            System.out.println(formDataAdvice.getOrder_id()+"1111111111");
            return interventionService.add(formDataAdvice,exerciseIds,foodIds);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"未知失败");
        }
    }

    /**
     * 通过干预id查询干预对象 用于编辑回显
     * @param interventionId
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer interventionId){
        try {
            FormDataAdvice formDataAdvice= interventionService.findById(interventionId);
            return new Result(true,"成功",formDataAdvice);
        }catch (Exception e){
            return new Result(false,"失败");
        }
    }

    /**
     * 回显id
     * @param interventionId
     * @return
     */
    @RequestMapping("/findexerciseIdsByinterventionId")
    public Result findExerciseIdsByInterventionId(Integer interventionId){
        try {
            List<Integer> list = interventionService.findexerciseIdsByinterventionId(interventionId);
            return new Result(true,"成功",list);
        }catch (Exception e){
            return new Result(false,"失败");
        }
    }

    /**
     * 回显id
     * @param interventionId
     * @return
     */
    @RequestMapping("/findfoodIdsByinterventionId")
    public Result findFoodIdsByInterventionId(Integer interventionId){
        try {
            List<Integer> list = interventionService.findfoodIdsByinterventionId(interventionId);
            return new Result(true,"成功",list);
        }catch (Exception e){
            return new Result(false,"失败");
        }
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody FormDataAdvice formDataAdvice, String[] exerciseIds, String[] foodIds){
        try {
            return interventionService.edit(formDataAdvice,exerciseIds,foodIds);
        }catch (Exception e){
            return new Result(false,"失败");
        }
    }
}
